package com.divine.wn.pt.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.divine.wn.pt.form.WriteReceiptForm;
import com.divine.wn.pt.vo.JlVO;
import com.divine.wn.pt.vo.JtVO;
import com.divine.wn.pt.vo.UserVO;

public class WriteReceiptAction extends BaseAction {

	// 业务员提交审批
	public ActionForward submitUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		WriteReceiptForm wForm = (WriteReceiptForm) form;
		UserVO userVO = wForm.getUserVO();

		if (StringUtils.isBlank(userVO.getId())) {
			String user_tableid = userService.insertUser(userVO).toString();
			
			

		} else {
			userService.updateUser(userVO);

		}
		
		request.setAttribute("submitUser", "学生资料维护成功！");

		return mapping.findForward("success");
	}

	public ActionForward getUserInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WriteReceiptForm wForm = (WriteReceiptForm) form;

		// 如果用户session失效，马上退回让用户重新登录

		if (backUser == null) {
			return mapping.findForward("fail");
		}

		 UserVO userVO = userService.getUser(request.getParameter("tableID"));
		 request.setAttribute("dataCount", userService.getdataCount(new HashMap())) ;
		 
		 wForm.setUserVO(userVO);

		return mapping.findForward("info");
	}
	
	public ActionForward toEditUserInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WriteReceiptForm wForm = (WriteReceiptForm) form;

		// 如果用户session失效，马上退回让用户重新登录

		if (backUser == null) {
			return mapping.findForward("fail");
		}

		 UserVO userVO = userService.getUser(request.getParameter("tableID"));
		 request.setAttribute("dataCount", userService.getdataCount(new HashMap())) ;
		 
		 wForm.setUserVO(userVO);

		return mapping.findForward("input");
	}

	// 转到填单页面
	public ActionForward toUserPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);

		if (backUser == null) {
			return mapping.findForward("fail");
		}
		
		WriteReceiptForm wForm = (WriteReceiptForm) form;
		
		int dataCount=userService.getdataCount(new HashMap());
		request.setAttribute("dataCount", dataCount);
		
		
		
		wForm.getUserVO().getJlList().add(new JlVO());
		
		wForm.getUserVO().getJtList().add(new JtVO());
		return mapping.findForward("input");
	}
	
	public ActionForward addJl(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		
		WriteReceiptForm wForm = (WriteReceiptForm) form;
		
		wForm.getUserVO().getJlList().add(new JlVO());
	
		return mapping.findForward("input");
	}
	
	public ActionForward addJt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);


		WriteReceiptForm wForm = (WriteReceiptForm) form;
		
		wForm.getUserVO().getJtList().add(new JtVO());
		return mapping.findForward("input");
	}
	
	public ActionForward deleteJl(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		
		WriteReceiptForm wForm = (WriteReceiptForm) form;
		
		wForm.getUserVO().getJlList().remove(new Integer(request.getParameter("jlId")).intValue());
	
		return mapping.findForward("input");
	}
	
	public ActionForward deleteJt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		WriteReceiptForm wForm = (WriteReceiptForm) form;
		
		wForm.getUserVO().getJtList().remove(new Integer(request.getParameter("jtId")).intValue());
		
		return mapping.findForward("input");
	}
	
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		
		userService.deleteUser(request.getParameter("tableID"));
		
		
		return returnToList(mapping, form, request, response);
	}
	
	
	
	
	public ActionForward returnToList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WriteReceiptForm wForm = (WriteReceiptForm) form;

		if (backUser == null) {
			return mapping.findForward("fail");
		}
		HashMap<String,Object> map = new HashMap<String,Object>();
		
		
		
		//封装搜索条件

		if(!StringUtils.isEmpty(wForm.getUserVO().getUser_name_search())){
			map.put("username", wForm.getUserVO().getUser_name_search());
		}
		
		if(wForm.getUserVO().getStart()!=null&&!wForm.getUserVO().getStart().equals("点击选择日期")){
			map.put("start", wForm.getUserVO().getStart());
		}
		
		
		
		if(wForm.getUserVO().getEnd()!=null &&!wForm.getUserVO().getEnd().equals("点击选择日期")){
			map.put("end", wForm.getUserVO().getEnd());
		}
		
		
	
		
		int dataCount = userService.getdataCount(map);
		request.setAttribute("dataCount", dataCount);
		
		resetSkip(request,request.getParameter("pager.offset"),map);
		request.setAttribute("total",dataCount );
		
		request.setAttribute("url","returnToList" );
		request.setAttribute("maxCount", wForm.getMaxCount());
		map.put("limit", wForm.getMaxCount());
		
		
		List<HashMap<String,Object>> dataList=null;
		if(dataCount==0){
			 dataList=new ArrayList<HashMap<String,Object>>();
		}else{
			dataList = userService.getdatalist(map);
		}
		
		request.setAttribute("dateReturnList", dataList);
		return mapping.findForward("waitList");
	}
	
	private void resetSkip(HttpServletRequest request,String offset,HashMap<String,Object> map){
		if(offset==null){
			map.put("skip", 0);
		}else{
			if(new Integer(offset)!=0){
				map.put("skip", new Integer(offset));
			}else{
				map.put("skip", 0);
			}			
		}
		
	}

}
